package crawler.web.framework;

import javax.annotation.Resource;

import library.Tracer;
import library.database.mongo.MongoDB;
import library.thread.flow.ParameterBox;
import library.thread.flow.WorkflowContainer;
import library.thread.flow.WorkflowNode;

import org.htmlparser.Parser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import crawler.web.beans.CrawlPageContext;
import crawler.web.engine.config.SpBN;
import crawler.web.engine.config.Suite;
import crawler.web.engine.custom.MongoDBAdapter;

/**
 * 從context0取html數據<br>
 * 将html解析为结构化的数据, 存入context1中<br>
 * 將垃圾數據丟棄
 * 
 * @author waynechen Feb 8, 2012 11:18:01 PM
 */
@Component("AnalyzerPrs")
@Scope(value = "prototype")
public class AnalyzerPrs extends WorkflowNode {
	@Autowired(required = true)
	public AnalyzerPrs(//
	@Value("#{T(crawler.web.framework.CrawlerContainer).getNO()}") String name, //
			@Qualifier("CrawlerContainer") WorkflowContainer<Crawler> container//
	) {
		super("AnalyzerPrs " + name, container);
	}

	private Parser parser = new Parser();
	private int waitTimes = 0;

	@Resource(name = SpBN.bean_MongoDB)
	private MongoDB mongo;

	@Resource(name = "MongoDBAdapter")
	private MongoDBAdapter dbAdapter;

	@Resource(name = "Dispatcher")
	private Dispatcher dispatcher;

	@Resource(name = SpBN.conf_htmlTabName)
	private String tableContext0;

	@Resource(name = SpBN.conf_parseTabName1)
	private String tableContext1;

	@Resource(name = SpBN.conf_table_empty_wait_time)
	private Integer waitTime;

	@Override
	protected void nodeExecute(ParameterBox args) {
		Tracer.info("线程: " + name + " 开始");

		ParameterBox params = new ParameterBox();
		params.push("parser", parser);
		params.push("caller", this);

		try {
			while (true) {
				if (isStopped())
					break;

				//处理消息
				handleMessage();

				CrawlPageContext pageContext = dbAdapter.getNext(//
				tableContext0, Suite.ParseStatus.DOWNLOAD, Suite.ParseStatus.PARSED);

				if (pageContext == null) {
					Tracer.info("分析库0中没有等待分析的数据,等待中...");
					sleepWithoutThrowing(10000);
					if (waitTimes++ > (waitTime / 10000))
						getContainer().sendMessage2me(this, WorkflowContainer.MSG_STOP_ALL_SUBNODES, null);
					continue;
				}
				waitTimes = 0;

				//解析html
				Tracer.info(pageContext.getUrl().getOriUrl());
				String html = pageContext.getUrl().getHtml();//.replaceAll("(<iframe.*?>)|(</iframe>)", "");
				Tracer.trace("html", html);
				parser.setInputHTML(html);
				parser.setEncoding(pageContext.getUrl().getEncoding());

				//删除不用的数据
				pageContext.getUrl().setHtml("");
				pageContext.getUrlList().clear();

				//进入分发器
				//哪个组件能处理,此url则处理此url
				params.push("pageContext", pageContext);
				dispatcher.dispatch(params);

				//输出信息
				Tracer.info("---------------" + name + "----->>>>>>>---------");
				Tracer.info(pageContext.getUrl().getLayer() + "层");
				Tracer.info(pageContext.getUrl().getOriUrl());
				Tracer.info(pageContext.getUrl());
				Tracer.trace("data", pageContext + "");

				//如果是垃圾数据,则丢弃
				if (pageContext.getPageType() == Suite.PageType.GARBAGE.getExpr())
					continue;

				//存入mongodb
				pageContext.setTagData(CrawlPageContext.nullTagData);//清空, 节省空间
				pageContext = mongo.save(tableContext1, pageContext);
			}
		} catch (Exception e) {
			Tracer.exception("线程: " + name + " 发生了异常");
			Tracer.exception(e);
		} finally {
			Tracer.info("线程: " + name + " 结束");
		}
	}

	@Override
	protected void handleMessage() {
	}
}

//
//
//
//
//
//
//
//
//
//
